package com.example.gamecenter.fragments.my

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.gamecenter.R
import com.example.gamecenter.adapter.ExploreRecyclerAdapter
import com.example.gamecenter.adapter.RecommendItem
import com.google.android.material.appbar.AppBarLayout
import kotlinx.android.synthetic.main.activity_my_page.rv_material_demo

class MyFragment: Fragment(R.layout.activity_my_page) {
    private lateinit var fragmentContext: Context;
    private lateinit var adapter : ExploreRecyclerAdapter;

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState);
        fragmentContext = (context ?: this) as Context;
        // 背景图
        val header_bg = view.findViewById<ImageView>(R.id.iv_header);
        val appBarLayout = view.findViewById<AppBarLayout>(R.id.abl_appbar);
        val page_title = view.findViewById<LinearLayout>(R.id.my_page_title)
        appBarLayout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _, verticalOffset ->
            // 在这里处理滚动事件
            println("verticalOffset:"+ verticalOffset)
            val maxH = dpToPx(160);
            val offsetY = Math.abs(verticalOffset).toDouble()
            val percentage = when {
                offsetY < maxH  -> (offsetY) / maxH
                offsetY >= maxH -> 1.0
                else -> 0.0
            }
            val bg_color = Color.argb((percentage*250).toInt(), 255, 255, 255);
            page_title.background = ColorDrawable(bg_color);
            val b = (percentage*250).toInt();
            println("滚动百分比: $b")
        })


        Glide.with(fragmentContext)
            .load("https://appimg-drcn.dbankcdn.com/application/screenshut1/c2ac1172d63e42b89227e127f916ff9f_13.jpg")
            .apply(RequestOptions().transform(CenterCrop(), RoundedCorners(30)))
            .into(header_bg);

        val data: List<RecommendItem> = listOf(
            RecommendItem("标题1", "https://img.sxsme.com.cn/uploadimg/ico/2023/1007/1696665441196246.jpg",1, "天剑传说"),
            RecommendItem("标题2", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552459611151.jpg",2, "天剑传说"),
            RecommendItem("标题3", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552498848724.jpg",3, "天剑传说"),
            RecommendItem("标题4", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552534375775.png",4, "天剑传说"),
            RecommendItem("标题5", "https://img.sxsme.com.cn/uploadimg/ico/2023/0228/1677552566865753.png",5, "天剑传说"),
            RecommendItem("标题6", "https://img.sxsme.com.cn/uploadimg/ico/2022/1214/1670986386653144.jpg",6, "天剑传说"),
            RecommendItem("标题7", "https://img.sxsme.com.cn/uploadimg/ico/2022/1214/1670986386653144.jpg",6, "天剑传说"),
            RecommendItem("标题8", "https://img.sxsme.com.cn/uploadimg/ico/2022/1214/1670986386653144.jpg",6, "天剑传说"),
        );
        setAdapter(true, false, data);
    }
    fun dpToPx(dp: Int): Int {
        val scale = resources.displayMetrics.density
        return (dp * scale + 0.5f).toInt()
    }
    /**
     * 参数1：设置布局是否垂直 true：垂直 false：水平
     * 参数2：设置布局的方向 true:反向 false：正常
     * */
    private fun setAdapter(isVertical: Boolean, isReverse: Boolean, data: List<RecommendItem> ) {

        //创建一个适配器对象，传一个集合进去给适配器
        adapter = ExploreRecyclerAdapter(data);
        //设置垂直还是水平，三元表达式
        val manager: LinearLayoutManager = LinearLayoutManager(fragmentContext);
        manager.orientation = if (isVertical) RecyclerView.VERTICAL else RecyclerView.HORIZONTAL

        // 设置方向
        manager.reverseLayout = false

        // 把设置好的方向和排版给 RecyclerView
        rv_material_demo.layoutManager = manager

        // 设置适配器
        rv_material_demo.adapter = adapter
    }
}